/*
 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any
 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
 * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that Software is not designed, licensed or intended
 * for use in the design, construction, operation or maintenance of
 * any nuclear facility.
 */

package com.sun.j2ee.blueprints.servicelocator;

import java.net.URL;

import javax.ejb.EJBHome;
import javax.ejb.EJBLocalHome;
import javax.jms.Queue;
import javax.jms.QueueConnectionFactory;
import javax.jms.Topic;
import javax.jms.TopicConnectionFactory;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.sql.DataSource;


/**
 * This class is an implementation of the Service Locator pattern. It is used to
 * looukup resources such as EJBHomes, JMS Destinations, etc. This
 * implementation uses the "singleton" strategy and also the "caching" strategy 
 * implemented as aspects.
 */
public class BaseServiceLocator {

	private InitialContext ic;

	protected BaseServiceLocator() throws ServiceLocatorException {	
		ic = new InitialContext();
	}

	/**
	 * will get the ejb Local home factory. If this ejb home factory has already
	 * been clients need to cast to the type of EJBHome they desire
	 * 
	 * @return the EJB Home corresponding to the homeName
	 */
	public EJBLocalHome getLocalHome(String jndiHomeName)
			throws ServiceLocatorException {		
		return (EJBLocalHome) ic.lookup(jndiHomeName);		
	}

	/**
	 * will get the ejb Remote home factory. If this ejb home factory has
	 * already been clients need to cast to the type of EJBHome they desire
	 * 
	 * @return the EJB Home corresponding to the homeName
	 */
	@SuppressWarnings("unchecked")
	public EJBHome getRemoteHome(String jndiHomeName, Class className)
			throws ServiceLocatorException {
		Object objref = ic.lookup(jndiHomeName);
		Object obj = PortableRemoteObject.narrow(objref, className);
		return (EJBHome) obj;
	}

	/**
	 * @return the factory for the factory to get queue connections from
	 */
	public QueueConnectionFactory getQueueConnectionFactory(
			String qConnFactoryName) throws ServiceLocatorException {	
			return(QueueConnectionFactory) ic.lookup(qConnFactoryName);		
	}

	/**
	 * @return the Queue Destination to send messages to
	 */
	public Queue getQueue(String queueName) throws ServiceLocatorException {		
		return (Queue) ic.lookup(queueName);
	
	}

	/**
	 * This method helps in obtaining the topic factory
	 * 
	 * @return the factory for the factory to get topic connections from
	 */
	public TopicConnectionFactory getTopicConnectionFactory(
			String topicConnFactoryName) throws ServiceLocatorException {
		return  (TopicConnectionFactory) ic.lookup(topicConnFactoryName);
	}

	/**
	 * This method obtains the topc itself for a caller
	 * 
	 * @return the Topic Destination to send messages to
	 */
	public Topic getTopic(String topicName) throws ServiceLocatorException {
			return  (Topic)ic.lookup(topicName);
	}

	/**
	 * This method obtains the datasource itself for a caller
	 * 
	 * @return the DataSource corresponding to the name parameter
	 */
	public DataSource getDataSource(String dataSourceName)
			throws ServiceLocatorException {
		return (DataSource) ic.lookup(dataSourceName);
	}

	/**
	 * @return the URL value corresponding to the env entry name.
	 */
	public URL getUrl(String envName) throws ServiceLocatorException {	
			return (URL) ic.lookup(envName);	
	}

	/**
	 * @return the boolean value corresponding to the env entry such as
	 *         SEND_CONFIRMATION_MAIL property.
	 */
	public boolean getBoolean(String envName) throws ServiceLocatorException {
			return ((Boolean) ic.lookup(envName)).booleanValue();		
	}

	/**
	 * @return the String value corresponding to the env entry name.
	 */
	public String getString(String envName) throws ServiceLocatorException {
		return (String) ic.lookup(envName);		
	}

}
